package com.extrace.ui.domain;

import android.content.Context;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.extrace.misc.model.ExpressSheet;
import com.extrace.net.IDataAdapter;
import com.extrace.ui.main.R;

import java.util.HashMap;
import java.util.List;

public class ExpressListAdapter1 extends ArrayAdapter<ExpressSheet> implements IDataAdapter<List<ExpressSheet>> {

    private List<ExpressSheet> itemList;
    private static HashMap<Integer, Boolean> isSelected;
    private Context context;

    public ExpressListAdapter1(List<ExpressSheet> itemList, Context context) {
        super(context, R.layout.express_check_item, itemList);

        this.context = context;
        this.itemList = itemList;
        isSelected = new HashMap<Integer, Boolean>();
        initData();
    }

    private void initData() {
        for (int i = 0; i < itemList.size(); i++)
            getIsSelected().put(i, false);
    }

    @Override
    public int getCount() {
        if(itemList == null) return 0;
        return itemList.size();
    }

    public ExpressSheet getItem(int position) {
        if(itemList != null)
            return itemList.get(position);
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        View v = convertView;
        ViewHolder hd = null;
        if(v==null) {
            hd = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.express_check_item, null);
            hd.cb = (CheckBox)v.findViewById(R.id.expressCheck);
            hd.name = (TextView)v.findViewById(R.id.name);
            hd.telCode = (TextView)v.findViewById(R.id.tel);
            hd.address = (TextView)v.findViewById(R.id.addr);
            hd.time = (TextView)v.findViewById(R.id.time);
            hd.id = (TextView)v.findViewById(R.id.id);

            v.setTag(hd);
        } else {
            hd = (ViewHolder)v.getTag();
        }

        ExpressSheet es = getItem(position);
        if(getIsSelected().get(position) != null)
            hd.cb.setChecked(getIsSelected().get(position));
        else hd.cb.setChecked(false);
        if(es.getSender() != null) {
            hd.name.setText(es.getSender().getName());
            hd.telCode.setText(es.getSender().getTelCode());
            hd.address.setText(es.getSender().getAddress());
        }
        if(es.getAccepteTime() != null){
            //SimpleDateFormat myFmt=new SimpleDateFormat("MM月dd日 hh:mm");
            hd.time.setText(DateFormat.format("MM月dd日 hh:mm",es.getAccepteTime()));
        }
        hd.id.setText(es.getID());

        return v;
    }

    @Override
    public List<ExpressSheet> getData() {
        return itemList;
    }

    @Override
    public void setData(List<ExpressSheet> data) {
        this.itemList = data;
    }

    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        ExpressListAdapter1.isSelected = isSelected;
    }

    public static class ViewHolder {
        CheckBox cb;
        TextView name;
        TextView telCode;
        TextView address;
        TextView time;
        TextView id;
    }
}
